# -*- coding: utf-8 -*-

from Command import *
import pygame
import os
import Renderer
import sys
from Kamaelia.UI.Pygame.KeyEvent import KeyEvent
from Kamaelia.Internet.TCPClient import TCPClient
from pygame.locals import *
from Axon.Component import component
from GameState import GameState
from Networking import Commandizer, Packetizer
from Kamaelia.UI.MH.PyGameApp import PyGameApp
from Kamaelia.UI.PygameDisplay import PygameDisplay
from Kamaelia.Util.Backplane import SubscribeTo
from Kamaelia.Chassis.Graphline import Graphline
from Kamaelia.Util.Console import ConsoleEchoer


# Create display
disp = PygameDisplay(width=640,height=480)
disp.activate()
PygameDisplay.setDisplayService(disp)
pygame.font.init()

# Create keyEvent handler
keyHandler = KeyEvent( key_events={ K_ESCAPE : ((COMMAND_ESCAPE,),"commandOutbox"),
                                K_w : ((COMMAND_MOVE_UP,),"commandOutbox"),
                                K_s : ((COMMAND_MOVE_DOWN,),"commandOutbox"),
                                K_a : ((COMMAND_MOVE_LEFT,),"commandOutbox"),
                                K_d : ((COMMAND_MOVE_RIGHT,),"commandOutbox"),
                                K_UP : ((COMMAND_MOVE_UP,),"commandOutbox"),
                                K_DOWN : ((COMMAND_MOVE_DOWN,),"commandOutbox"),
                                K_LEFT : ((COMMAND_MOVE_LEFT,),"commandOutbox"),
                                K_RIGHT : ((COMMAND_MOVE_RIGHT,),"commandOutbox"),
                                K_RETURN : ((COMMAND_SWITCH,),"commandOutbox"),
                                K_SPACE : ((COMMAND_SET_READY,),"commandOutbox"),
                                },
                    outboxes={ 
                               "commandOutbox" : "commands outbox",
                             }
                  )
                  
updater = Renderer.UpdateGenerator(10)
boardRenderer = Renderer.BoardRenderer()
gameState = GameState() 
                  
Graphline(UPDATER  = updater,
          BRENDERER = boardRenderer,
          
          COMMANDIZER = Commandizer(),
          PACKETIZER  = Packetizer(),    
          GAME_STATE = gameState,
          TCP_CLIENT = TCPClient("127.0.0.1", 6662),
          KEY_HANDLER = keyHandler,
                                   
          linkages = {
             # Updater to renderer
             ("UPDATER",     "outbox") : ("BRENDERER", "updateInbox"),
             
             # Keys to networking             
             ("KEY_HANDLER","commandOutbox") : ("COMMANDIZER","writeInbox"),
             #("KEY_HANDLER","commandOutbox") : ("ECHO","inbox"),
             
             # Networking to game state
             ("COMMANDIZER","readOutbox") : ("GAME_STATE", "CommandInbox"),
             
             # Game state to renderer
             ("GAME_STATE", "RenderOutbox") : ("BRENDERER","renderInbox"), 
             
             # Networking
             ("COMMANDIZER", "writeOutbox") : ("PACKETIZER","writeInbox"),
             ("PACKETIZER", "writeOutbox") : ("TCP_CLIENT","inbox"),
                  
             ("TCP_CLIENT", "outbox") : ("PACKETIZER","readInbox"),
             ("PACKETIZER", "readOutbox") : ("COMMANDIZER","readInbox"),
             
          }).run()        